from inspect import getmodule
import logging
import requests
import download
import time

login_res=None
from myglobal import global_session
LCDP="http://10.0.108.4:3000/hanson-lcdp"
def get_oapi():
    # 创建global_session对象
    # 设置global_session级别的超时
  
    data = {'projectId': 'n7hnngimnxalqi2ih8euj59z3upscog1'}
    try:
        # 使用global_session发送请求
        response = global_session.post("http://10.0.108.4:3000/hanson-lcdp/oapi/ncstest/test/body1", 
                              json=data)
        response.raise_for_status()
        print(response.json())
        print("ok")
        print(response.json())
    except requests.exceptions.Timeout:
        print("请求超时")
    except requests.exceptions.RequestException as e:
        print("error")
        print(e)
def getModule():
    data={}
    data["_t"]=int(time.time())
    data["token"]=login_res["data"]["token"]
    data["projectId"]="n7hnngimnxalqi2ih8euj59z3upscog1"
    logging.info(data)
    url="/module/getAllModuleTreeUseByRoleIds"
    url=LCDP+url
    try:
        response = global_session.get(url,json=data)
        response.raise_for_status()
        res=response.json()
        logging.info(res)
    except requests.exceptions.RequestException as e:
        print("error")
        print(e)
def get_module_tree():
    """
    获取模块树数据的Python实现
    """
    import time
    headers = {
        'Content-Type': 'application/json',
        'x-access-token': login_res['data']['token']
    }
    data = {
        "_t": int(time.time()),  # 当前时间戳
        "projectId": "n7hnngimnxalqi2ih8euj59z3upscog1",
        "token": login_res["data"]["token"]
    }
    
    url = "http://10.0.108.4:3000/hanson-lcdp/module/getAllModuleTreeUseByRoleIds"
    
    try:
        response = global_session.get(url, params=data, headers=headers)
        response.raise_for_status()
        
        print("模块树数据:", response.json())
        return response.json()
        
    except requests.exceptions.RequestException as e:
        print("获取模块树失败:", e)
        return None
def login():
    global login_res
    data={}
    data["username"]="gte"
    data["password"]="hx123456."
    try:
        response = global_session.post("http://10.0.108.4:3000/hanson-lcdp/sys/login", json=data)
        response.raise_for_status()
        login_res=response.json()
    except requests.exceptions.RequestException as e:
        print("error")
        print(e)
def get_jindu():
    data = {'hth': ''}
    try:
        response = global_session.post("http://10.0.108.4:3000/hanson-lcdp/oapi/ncstest/test/jindu", json=data, 
                                timeout=(3, 5))
        response.raise_for_status()
        print(response.json())
        print("ok")
        print(response.json())
    except requests.exceptions.RequestException as e:
        print("error")
        print(e)
def getDoc():        
    data = {"reqId":"328432","page":{"pageSize":20,"current":1}}
    try:
        response = global_session.post("http://10.0.108.4:3000/hanson-lcdp/oapi/checkLims10701/getDoc10701/", json=data)
        response.raise_for_status()
        res=response.json()
        datarows=res["data"]["result"]
        for one in datarows:
            print(one["FILE_PATH"])
            download.download_path(one["FILE_PATH"])
    except requests.exceptions.RequestException as e:
        print("error")
        print(e)

def test():
    get_oapi()
def getResult():
    data={
      "interfaceId": "oapi:e4795174abe2417784e11d20f3844fe8",
      "reqId": 550845,
      "flowTaskId": 5625174,
      "methodIds": "103",
      "ids": "14615080",
      "sMethodIds": "295",
      "sAnalysisSkillIds": "274",
      "text": "GB/T 20123-2006"
    }
    try:
        url="http://10.0.108.4:3000/hanson-lcdp/oapi/testFlow/resultHandle/getResultHandleSampleFilterByMethods"
        response = global_session.post(url, json=data)
        response.raise_for_status()
        print(response.json())
        print("ok")
        print(response.json())
    except requests.exceptions.RequestException as e:
        print("error")
        print(e)
def fei():
    # http://10.0.108.4:3000/hanson-lcdp/interface/invoking
    # {
    #   "interfaceId": "da511e107841afcd1bbf664760f41ac4",
    #   "docId": "2084023",
    #   "flowTaskId": 5625174
    # }
    # {
    #     "msg": "操作成功！",
    #     "code": "00000",
    #     "data": {
    #         "id": "",
    #         "paramMap": {
    #             "resultList": [
    #                 {
    #                     "res": "操作成功！"
    #                 }
    #             ]
    #         }
    #     },
    #     "message": "操作成功！"
    # }
    # 作废
    pass
def shengcheng():
    pass
    # {
    #   "interfaceId": "12cc243f38eb5e2cfc466694110d4b38",
    #   "methodIds": "103"
    # }
    # {
    #     "msg": "操作成功！",
    #     "code": "00000",
    #     "data": {
    #         "id": "",
    #         "paramMap": {
    #             "resultList": [
    #                 {
    #                     "templateId": "5d79bdd6b6fba35309e4f77a9d4dac3e",
    #                     "descript": "",
    #                     "chName": "24沈阳CSONH气体原始记录",
    #                     "fileType": "Excel版本"
    #                 },
    #                 {
    #                     "templateId": "2f52d22a5b6771be356c4d26fd62e819",
    #                     "descript": "",
    #                     "chName": "csonh_new",
    #                     "fileType": "Excel版本"
    #                 },
    #                 {
    #                     "templateId": "aa82c2b6f53e03cec7c55e8a024c2544",
    #                     "descript": "ONH原始记录",
    #                     "chName": "ONH原始记录",
    #                     "fileType": "Excel版本"
    #                 }
    #             ]
    #         }
    #     },
    #     "message": "操作成功！"
    # }
# getResult()
# getDoc()
# get_jindu()
def main():
    login()
    logging.info(login_res)
    if login_res["code"]=="00000":
        # get_jindu()
        # getModule()
        get_module_tree()
if __name__=="__main__":
    main()
